﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace xTeam.Glgcx.CommonUtilities
{
    public static class ClassifyExtend
    {
        /// <summary>
        /// 生成树
        /// </summary>
        /// <param name="classifyList"></param>
        /// <returns></returns>
        public static string ClassTree(List<EClassify> classifyList)
        {
            StringBuilder sb = new StringBuilder();
            Action<List<EClassify>, int> tree = null;

            tree = (List<EClassify> x, int y) =>
            {
                var temp = classifyList.Select(i =>
                {
                    if (i.ParentId == y)
                    {
                        sb.Append("<li>");
                        sb.Append("<div class=\"overFlowHidden\">");
                        sb.Append("<div class=\"floatRight\">");
                        sb.Append("<input type=\"text\" disabled=\"disabled\" value=\"" + i.Sequence + "\" class=\"width50 textCenter\" />");
                        sb.Append("<input type=\"button\" value=\"编辑\" name=\"btnEdit\" onclick=\"Edit(" + i.ClassifyId + ")\" />");
                        sb.Append("<input type=\"button\" value=\"删除\" name=\"btnDelete\" onclick=\"Delete(" + i.ClassifyId + ")\" />");
                        sb.Append("</div>");
                        sb.Append(i.Title);
                        sb.Append("</div>");
                        if (classifyList.Count(n => n.ParentId == i.ClassifyId) > 0)
                        {
                            sb.Append("<ul>");
                            tree(classifyList, i.ClassifyId);
                            sb.Append("</ul>");
                        }
                        sb.Append("</li>");

                    }
                    return false;
                });
                foreach (var c in temp) ;
            };
            //sb.Append("<ul>");
            if (classifyList.Count > 0)
            {
                tree(classifyList, classifyList.Min(i => i.ParentId));
            }
            //sb.Append("</ul>");
            return sb.ToString();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="classifyList"></param>
        /// <param name="selectedValue"></param>
        /// <param name="please"></param>
        /// <returns></returns>
        public static string DropDownlist(string name, List<EClassify> classifyList, int selectedValue, bool please)
        {
            StringBuilder sb = new StringBuilder();
            Action<List<EClassify>, int> tree = null;

            tree = (List<EClassify> x, int y) =>
            {
                var temp = classifyList.Select(i =>
                {
                    if (i.ParentId == y)
                    {
                        sb.Append("<li>");
                        sb.Append("<div class=\"overFlowHidden\">");
                        sb.Append("<label>");
                        sb.AppendFormat("<input type=\"radio\" title=\"{0}\"", i.Title);
                        if (selectedValue == i.ClassifyId)
                            sb.Append(" checked=\"checked\"");
                        sb.Append(" name=\"" + name + "\"");
                        sb.Append(" value=\"" + i.ClassifyId + "\" />");

                        sb.Append(i.Title);
                        sb.Append("</label>");
                        sb.Append("</div>");
                        if (classifyList.Count(n => n.ParentId == i.ClassifyId) > 0)
                        {
                            sb.Append("<ul>");
                            tree(classifyList, i.ClassifyId);
                            sb.Append("</ul>");
                        }
                        sb.Append("</li>");

                    }
                    return false;
                });
                foreach (var c in temp) ;
            };
            //sb.Append("<ul>");
            if (please)
            {
                sb.Append("<li>");
                sb.Append("<div class=\"overFlowHidden\">");
                sb.Append("<label>");
                sb.AppendFormat("<input type=\"radio\" title=\"顶级分类\"");
                sb.Append(" checked=\"checked\"");
                sb.Append(" name=\"" + name + "\"");
                sb.Append(" value=\"0\" />");
                sb.Append("顶级分类");
                sb.Append("</label>");
                sb.Append("</div>");
            }
            if (classifyList.Count > 0)
            {
                tree(classifyList, classifyList.Min(i => i.ParentId));
            }
            //sb.Append("</ul>");
            return sb.ToString();

        }
        /// <summary>
        /// 类别选择器，只有最有一级类别可选
        /// </summary>
        /// <param name="name"></param>
        /// <param name="selectedValue"></param>
        /// <param name="classifyList"></param>
        /// <returns></returns>
        public static string ParentClassify(string name, int selectedValue, List<EClassify> classifyList)
        {
            StringBuilder sb = new StringBuilder();
            Action<List<EClassify>, int> tree = null;

            tree = (List<EClassify> x, int y) =>
            {
                var temp = classifyList.Select(i =>
                {
                    if (i.ParentId == y)
                    {
                        sb.Append("<li>");
                        sb.Append("<div class=\"overFlowHidden\">");
                        sb.Append("<label>");
                        sb.AppendFormat("<input type=\"radio\" title=\"{0}\"", i.Title);
                        if (classifyList.Count(n => n.ParentId == i.ClassifyId) > 0)
                            sb.Append(" disabled=\"disabled\"");
                        if (selectedValue == i.ClassifyId)
                            sb.Append(" checked=\"checked\"");
                        sb.Append(" name=\"" + name + "\"");
                        sb.Append(" value=\"" + i.ClassifyId + "\" />");

                        sb.Append(i.Title);
                        sb.Append("</label>");
                        sb.Append("</div>");
                        if (classifyList.Count(n => n.ParentId == i.ClassifyId) > 0)
                        {
                            sb.Append("<ul>");
                            tree(classifyList, i.ClassifyId);
                            sb.Append("</ul>");
                        }
                        sb.Append("</li>");

                    }
                    return false;
                });
                foreach (var c in temp) ;
            };
            //sb.Append("<ul>");
            sb.Append("<li>");
            sb.Append("<div class=\"overFlowHidden\">");
            sb.Append("<label>");
            sb.AppendFormat("<input type=\"radio\" title=\"请选择分类\"");
            sb.Append(" checked=\"checked\" disabled=\"disabled\"");
            sb.Append(" name=\"" + name + "\"");
            sb.Append(" value=\"0\" />");
            sb.Append("请选择分类");
            sb.Append("</label>");
            sb.Append("</div>");
            // 最底层
            tree(classifyList, classifyList.Min(i => i.ParentId));
            //sb.Append("</ul>");
            return sb.ToString();

        }
    }
}
